/*
 * @Description: 
 * @Version: 1.0
 * @Author: wenbin
 * @Date: 2021-03-29 15:07:02
 * @LastEditors: wenbin
 * @LastEditTime: 2021-05-31 10:44:27
 * @FilePath: /magus-plugin/common/utils/catch.js
 * Copyright (C) 2021 wenbin. All rights reserved.
 */

//定时 缓存
var magusCloudCatch = {
  Cache: {
    /**
     * 总容量5M
     * 存入缓存，支持字符串类型、json对象的存储
     * 页面关闭后依然有效 ie7+都有效
     * @param key 缓存key
     * @param stringVal
     * @time 数字 缓存有效时间（秒） 默认10年（永久）
     * 注：localStorage 方法存储的数据没有时间限制。第二天、第二周或下一年之后，数据依然可用。不能控制缓存时间，故此扩展
     * */
    getNoExpireTime: function (key) {
      if (!localStorage) {
        return false;
      }
      return localStorage.getItem(key);
    },
    putNoExpireTime: function (key, stringVal) {
      try {
        if (!localStorage) {
          return false;
        }
        return localStorage.setItem(key, stringVal);
      } catch (e) {
        console.log(e);
      }
    },
    put: function (key, stringVal, time) {
      try {
        if (!localStorage) {
          return false;
        }
        if (!time || isNaN(time)) {
          time = 3650 * 24 * 60 * 60;
        } //10年
        var cacheExpireDate = new Date() - 1 + time * 1000;
        var cacheVal = {
          val: stringVal,
          exp: cacheExpireDate
        };
        localStorage.setItem(key, JSON.stringify(cacheVal)); //存入缓存值
        //console.log(key+":存入缓存，"+new Date(cacheExpireDate)+"到期");
      } catch (e) {
        console.log(e);
      }
    },
    /**获取缓存*/
    get: function (key) {
      try {
        if (!localStorage) {
          return false;
        }
        var cacheVal = localStorage.getItem(key);
        var result = JSON.parse(cacheVal);
        var now = new Date() - 1;
        if (!result) {
          return null;
        } //缓存不存在
        if (now > result.exp) {
          //缓存过期
          this.remove(key);
          return '';
        }
        //console.log("get cache:"+key);
        return result.val;
      } catch (e) {
        this.remove(key);
        return null;
      }
    },
    /**移除缓存，一般情况不手动调用，缓存过期自动调用*/
    remove: function (key) {
      if (!localStorage) {
        return false;
      }
      localStorage.removeItem(key);
    },
    /**清空所有缓存*/
    clear: function () {
      if (!localStorage) {
        return false;
      }
      localStorage.clear();
    },
    /**浏览器最大可用*/
    max: function () {},
    /**当前最大可用*/
    last: function () {},
    /**已用*/
    used: function () {}
  } //end Cache
};

// // 获取缓存
// export function getLocalItem(key, needEmpireTime) {
//   return needEmpireTime
//     ? magusCloudCatch.Cache.get(key)
//     : magusCloudCatch.Cache.getNoExpireTime(key);
// }

// // 设置缓存
// export function setLocalItem(key, value, time) {
//   time
//     ? magusCloudCatch.Cache.put(key, value, time)
//     : magusCloudCatch.Cache.putNoExpireTime(key, value);
// }

// // 移除缓存
// export function removeLocalItem(key) {
//   magusCloudCatch.Cache.remove(key);
// }

// // 清除所有缓存
// export function clearAll() {
//   magusCloudCatch.Cache.clear();
// }


let catchUtil = {
  getLocalItem: function (key, needEmpireTime) {
    return needEmpireTime ?
      magusCloudCatch.Cache.get(key) :
      magusCloudCatch.Cache.getNoExpireTime(key);
  },
  setLocalItem: function (key, value, time) {
    time
      ?
      magusCloudCatch.Cache.put(key, value, time) :
      magusCloudCatch.Cache.putNoExpireTime(key, value);
  },
  removeLocalItem: function (key) {
    magusCloudCatch.Cache.remove(key);
  },
  clearAll: function () {
    magusCloudCatch.Cache.clear();
  },
  setSession: function (name, value) {
    if (window.opener && Object.getOwnPropertyNames(window.opener).length > 0) {
      window.opener.sessionStorage.setItem(name, value)
    } else {
      sessionStorage.setItem(name, value)
    }
  },
  getSession: function (name) {
    if (window.opener && Object.getOwnPropertyNames(window.opener).length > 0) {
      return window.opener.sessionStorage.getItem(name)
    } else {
      return sessionStorage.getItem(name)
    }
  }
}

export default catchUtil;